//
//  AppDelegate+AlibabaCloudPush.m
//  MyApp
//
//  Created by apple on 2017/6/20.
//
//

#import "AppDelegate+AlibabaCloudPush.h"
#import <objc/runtime.h>
#import <CloudPushSDK/CloudPushSDK.h>
#import <UserNotifications/UserNotifications.h>

@interface AppDelegate () {

}

@end

@implementation AppDelegate (AlibabaCloudPushCategory)



+(void)load{
    
    Method origin;
    Method swizzle;
    
    origin=class_getInstanceMethod([self class],@selector(init));
    swizzle=class_getInstanceMethod([self class], @selector(init_plus));
    method_exchangeImplementations(origin, swizzle);
}

-(instancetype)init_plus{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidLaunch:) name:UIApplicationDidFinishLaunchingNotification object:nil];
    self.alibabaCloudPush = [[AlibabaCloudPush alloc] init];
    
    [self.alibabaCloudPush initCloudPush];
    UIApplication *application = [UIApplication sharedApplication];
    [self.alibabaCloudPush registerAPNS:application];
    [self registerMessageReceive];
    
    return [self init_plus];
}

-(void)applicationDidLaunch:(NSNotification *)notification{
    AppDelegate *notificationInfoDelegete = [UIApplication sharedApplication].delegate;
    if (notification) {

//        CCPSysMessage *message = notification.userInfo;
//        NSError * error = nil;
        @try{
//        NSData * jsonData = [NSJSONSerialization dataWithJSONObject:notification.userInfo options:NSJSONWritingPrettyPrinted error:&error];
//        NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:jsonStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"打开app进行初始化",notificationInfoDelegete.notificationInfo[@"content"], nil];
//            [alert show];
//            [notificationInfoDelegete.notificationInfo setValue:notification.userInfo[@"UIApplicationLaunchOptionsRemoteNotificationKey"][@"aps"][@"alert"][@"title"] forKey:@"title"];
//            [notificationInfoDelegete.notificationInfo setValue:notification.userInfo[@"UIApplicationLaunchOptionsRemoteNotificationKey"][@"aps"][@"alert"][@"content"] forKey:@"content"];
//            [notificationInfoDelegete.notificationInfo setValue:notification.userInfo[@"UIApplicationLaunchOptionsRemoteNotificationKey"][@"messageId"] forKey:@"messageId"];
            notificationInfoDelegete.notificationInfo = notification.userInfo[@"UIApplicationLaunchOptionsRemoteNotificationKey"] ;
        }@catch(NSException * error){
        
        }
//            [notificationInfoDelegete.notificationInfo setValue:@"true" forKey:@"isFirstEnter"];
        
//        @try{
//            NSString *title = [[NSString alloc] initWithData:message.title encoding:NSUTF8StringEncoding];
//            NSString *body = [[NSString alloc] initWithData:message.body encoding:NSUTF8StringEncoding];
//            [notificationInfoDelegete.notificationInfo setValue:body forKey:@"content"];
//            [notificationInfoDelegete.notificationInfo setValue:title forKey:@"title"];
//        }
//        @catch(NSException * error){
//            notificationInfoDelegete.notificationInfo = [NSMutableDictionary dictionary];
//            [notificationInfoDelegete.notificationInfo setValue:@"啥都没" forKey:@"test"];
//        }
//            [notificationInfoDelegete.notificationInfo setValue:@"wodeceshi" forKey:@"test"];
        
        
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:notificationInfoDelegete.notificationInfo[@"title"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"打开app进行初始化",notificationInfoDelegete.notificationInfo[@"content"], nil];
//        [alert show];
        [CloudPushSDK sendNotificationAck:notification.userInfo];
    }
}

/*
 *  苹果推送注册成功回调，将苹果返回的deviceToken上传到CloudPush服务器
 */
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [self.alibabaCloudPush didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
/*
 *  苹果推送注册失败回调
 */
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError %@", error);
    [self.alibabaCloudPush didFailToRegisterForRemoteNotificationsWithError:error];
}

/**
 *    注册推送消息到来监听
 */
- (void)registerMessageReceive {
    [self.alibabaCloudPush registerMessageReceive];
}

/*
 *  App处于启动状态时，通知打开回调
 */
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo {
    if (@available(iOS 10.0, *)) {
        
    } else {
        [self.alibabaCloudPush didReceiveRemoteNotification:userInfo];
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"App处于启动状态时，通知打开回调" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
//        [alert show];
//        @try{
//            NSError * error = nil;
//            NSData * jsonData = [NSJSONSerialization dataWithJSONObject:userInfo options:NSJSONWritingPrettyPrinted error:&error];
//            NSString * jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:jsonStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"App处于启动状态时，通知打开回调", nil];
//            [alert show];
//
//        }
//        @catch(NSException * error){
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"bushizidian" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"App处于启动状态时，通知打开回调", nil];
//            [alert show];
//        }
    }
   
}
    
    
@end


@implementation JSONUtil

+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
        
    }
    
    return dic;
    
}

+ (NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}

@end
